package com.freemalll.merchant.utils;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.ssl.SSLContextBuilder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLContext;
import java.math.BigDecimal;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class AESUtil {
	private static final String AES = "AES";

	/**
	 * 加密
	 * 
	 * @param src
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] src, String key) throws Exception {
		Cipher cipher = Cipher.getInstance(AES);
		SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), AES);
		cipher.init(Cipher.ENCRYPT_MODE, securekey);// 设置密钥和加密形式
		return cipher.doFinal(src);
	}

	/**
	 * 解密
	 * 
	 * @param src
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] src, String key) throws Exception {
		Cipher cipher = Cipher.getInstance(AES);
		SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), AES);// 设置加密Key
		cipher.init(Cipher.DECRYPT_MODE, securekey);// 设置密钥和解密形式
		return cipher.doFinal(src);
	}

	/**
	 * 二行制转十六进制字符串
	 *
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	public static byte[] hex2byte(byte[] b) {
		if ((b.length % 2) != 0)
			throw new IllegalArgumentException("长度不是偶数");
		byte[] b2 = new byte[b.length / 2];
		for (int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			b2[n / 2] = (byte) Integer.parseInt(item, 16);
		}
		return b2;
	}

	/**
	 * 解密
	 *
	 * @param data
	 * @return
	 * @throws Exception
	 */

	public final static String decrypt(String data, String key) {
		try {
			return new String(decrypt(hex2byte(data.getBytes()), key));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 加密
	 *
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public final static String encrypt(String data, String key) {
		try {
			return byte2hex(encrypt(data.getBytes(), key));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 创建SSL安全连接
	 * 
	 * @return
	 */
	public static SSLConnectionSocketFactory createSSLConnSocketFactory() {
		SSLConnectionSocketFactory sslsf = null;
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					return true;
				}
			}).build();
			sslsf = new SSLConnectionSocketFactory(sslContext);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		return sslsf;
	}

	public static void main(String[] args) {
		// String content = "{\n" + " \"username\":\"gimi\",\n" + "
		// \"password\":\"1211425\"\n" + "}";
		String key = "123456ufq1234567";
		// String idEncrypt = encrypt(content, key);
		// System.out.println(idEncrypt);
		String idDecrypt = decrypt(
				"eyJhbGciOiJIUzI1NiJ9.eyJleHQiOjE1MTEyNDQ1NDMxMjQsInVpZCI6InVmcSIsImlhdCI6MTUxMTE1ODE0MzEyNCwidmVyc2lvbiI6IjEuMCJ9.6-m82uiRERc7GAx_UZIw4JUNr0_S6jcLZIanAa_RycE",
				key);
		System.out.println(idDecrypt);
	}

	public static String getAmountRange(BigDecimal amount) {
		String range = "";
		if (amount.compareTo(new BigDecimal("0")) > 0 && amount.compareTo(new BigDecimal("1000")) <=0) {
			range = "(0,1000]";
		} else if (amount.compareTo(new BigDecimal("1000")) > 0 && amount.compareTo(new BigDecimal("5000")) <= 0) {
			range = "(1000,5000]";
		} else if (amount.compareTo(new BigDecimal("5000")) > 0 && amount.compareTo(new BigDecimal("10000")) <= 0) {
			range = "(5000,10000]";
		} else if (amount.compareTo(new BigDecimal("10000")) > 0 && amount.compareTo(new BigDecimal("20000")) <= 0) {
			range = "(10000,20000]";
		} else if (amount.compareTo(new BigDecimal("20000")) > 0 && amount.compareTo(new BigDecimal("50000")) <= 0) {
			range = "(20000,50000]";
		} else if (amount.compareTo(new BigDecimal("50000")) > 0 && amount.compareTo(new BigDecimal("100000")) <= 0) {
			range = "(50000,100000]";
		}
		return range;
	}
}
